var sass = {
    parent: window.top,

    isAll: function (obj) {
        var tabF = document.getElementsByClassName('tab1')[0];
        var inputCheck = parent.$(tabF).find('input[type="checkbox"]');
        var isChecked = obj.attr('checked');
        
        if (isChecked === 'checked' && inputCheck.length > 1) {
            for (var i = 1; i < inputCheck.length; i++) {
                inputCheck.eq(i).attr('checked', true);
            }
        }

        if (isChecked !== 'checked' && inputCheck.length > 1) {
            for (var i = 1; i < inputCheck.length; i++) {
                inputCheck.eq(i).attr('checked', false);
            }
        }
        
    },

    totalIsCheck: function () {
        var tabF = document.getElementsByClassName('tab1')[0];
        var inputCheck = parent.$(tabF).find('input[type="checkbox"]');
        var report = {
            count: 0,
            index: 0
        };

        for (var i = 1; i < inputCheck.length; i++) {
            if (inputCheck.eq(i).attr('checked')) {
                ++report.count;
                report.index = i;
            }
        }

        return report;
    },

    checkListener: function () {
        var tabF = document.getElementsByClassName('tab1')[0];
        var inputCheck = parent.$(tabF).find('input[type="checkbox"]');
        var _this = this;

        for (var i = 1; i < inputCheck.length; i++) {
            inputCheck.eq(i).on('click', function() {
                _this.setCheck();
            });
        }
    },

    getListIndex: function() {
        var tabF = document.getElementsByClassName('tab1')[0];
        var inputCheck = parent.$(tabF).find('input[type="checkbox"]');
        var list = [];

        for (var i = 1; i < inputCheck.length; i++) {
            if (inputCheck.eq(i).attr('checked')) {
                list.push({
                    id:inputCheck.eq(i).data('id'),
                    productId: inputCheck.eq(i).data('productid')
                });
            }
        } 

        return list;
    },

    setCheck: function () {
        var tabF = document.getElementsByClassName('tab1')[0];
        var inputCheck = parent.$(tabF).find('input[type="checkbox"]');
        var count = 0;

        for (var k = 1; k < inputCheck.length; k++) {
            if(inputCheck.eq(k).attr('checked')) {
                ++count;
            }
        }

        if (count === inputCheck.length - 1) {
            inputCheck.eq(0).attr('checked', 'checked');
        } else {
            inputCheck.eq(0).removeAttr('checked');
        }
    },

    showHide: function (obj) {

        if (obj.run !== 'undefined' || obj.run !== true) {
            return false;
        }

        obj.run = false;

        var formset = document.getElementsByClassName('sass-from-set')[0];
        var strenth = formset.getAttribute('s');

        if ( strenth !== 'true') {
            formset.setAttribute('h', formset.clientHeight);
            formset.setAttribute('s', 'true');
            obj.html('展开筛选');
            parent.$(formset).animate({height: 65}, { complete: function() {
                obj.run = true;
            }});
        } else {
            var ch = parseInt(formset.getAttribute('h'));
            formset.setAttribute('s', 'false');
            obj.html('收起筛选');
            parent.$(formset).animate({height: ch}, { complete: function() {
                obj.run = true;
            }});
        }
    },


    init: function () {
        var _this = this;
        parent.$(document).on('click','#check', function() {
            _this.isAll(parent.$(this));
        });
        
        _this.checkListener();
    }
}